<template>
<!--语音通话-->
  <div>
      <div>
        <span>请输入会议号：</span>
        <input type="text"></input>
        <van-button type="info" id="join">加入会议</van-button>
      </div>
  </div>
</template>

<script>
const AgoraRTC  = require('agora-rtc-sdk-ng');

export default {
  name: "voice",
  data() {
    return {
      rtc:{
        localAudioTrack: null,
        client: null,
      },
      options:{
        appId: "93d21a279e4f4270906ac78ac18d88c7",
        channel: "test",
        token: "00693d21a279e4f4270906ac78ac18d88c7IACqyFRO19w9i1FA14Q4faXe/6Ytg1nVBZloLPdpLQL9sQx+f9gAAAAAEAC0DMoWcTzFYQEAAQBzPMVh",
      },
      }
  },
  mounted() {
    this.startBasicCall()
  },
  methods:{
    async startBasicCall(){
      let that = this;
      //"rtc"通信场景  "vp8"编码格式
      that.rtc.client = AgoraRTC.createClient({mode:"rtc",codec:"vp8"});
      // document.getElementById("join")
      //加入频道
      await that.rtc.client.join(that.options.appId,that.options.channel,that.options.token)
      //用户加入频道回调
      that.rtc.client.on("user-joined", async (user) => {
        //订阅远端用户的音视频轨道
      })
      //远端用户发布了音视频轨道
      that.rtc.client.on("user-published", async (user, mediaType) => {
        //订阅远端用户的音视频轨道
        await that.rtc.client.subscribe(user, mediaType);
        if (mediaType === "video") {
          console.log("发起了语音通话");
          //播放远端音频轨道
          user.videoTrack.play("xxx");
        }
        if (mediaType === "audio") {
          console.log("发起了视频通话");
          user.audioTrack.play();
        }
      })
      //远端用户取消了音视频轨道
      that.rtc.client.on("user-unpublished", async (user, mediaType) => {
        //订阅远端用户的音视频轨道
        await that.rtc.client.subscribe(user, mediaType);
        if (mediaType === "video") {
          console.log("已取消了语音通话");
          user.videoTrack.play("xxx");
        }
        if (mediaType === "audio") {
          console.log("已取消了视频通话");
          user.audioTrack.play();
        }
      })

    }
  },
}
</script>

<style scoped>

</style>